package com.yokit.resource_management.framework.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yokit.resource_management.dto.ResponseDto;
import com.yokit.resource_management.entity.Room;
import com.yokit.resource_management.framework.service.RoomService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fengzeng
 * @create 2021/6/25 2:58
 */
@RestController
public class RoomController {


    @Resource
    private RoomService roomService;
    private String seaRoom;


    /**
     * @return
     * @Description 查询所有会议室
     * @Date ${time} ${date}
     * @Param pageNum 分页页数  pageSize 分页数
     **/
    @RequestMapping(value = "/roomList", method = RequestMethod.GET)
    public Map getRoomList(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Room> rooms = roomService.selectAll();
        PageInfo pageInfo = PageInfo.of(rooms);
        Map map = new HashMap();
        map.put("data", pageInfo.getList());
        map.put("status", 200);
        map.put("msg", "获取会议室列表成功");
        map.put("total", pageInfo.getTotal());
        return map;
    }

    /**
     * 按名称模糊查找会议室
     *
     * @param seaRoom  searchroom
     * @return
     */
    @RequestMapping(value = "/searchroom", method = RequestMethod.GET)
    public Map getRoomByName(@RequestParam(value = "searchRoom") String seaRoom,@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        this.seaRoom = seaRoom;
        String searchName = seaRoom;
        PageHelper.startPage(pageNum, pageSize);
        List<Room> rooms = roomService.selectRoomById(searchName);
        PageInfo pageInfo = PageInfo.of(rooms);
        Map map = new HashMap();
        map.put("data", pageInfo.getList());
        map.put("status", 200);
        map.put("msg", "查询会议室列表成功");
        map.put("total", pageInfo.getTotal());
        return map;
    }


    /**
     * @return
     * @Description 添加会议室
     * @Date
     * @Param adrm addroom
     **/
    @RequestMapping(value = "/addroom", method = RequestMethod.POST)
    public ResponseDto addrooms(@RequestBody Map<String, Object> addRoom) {
        String roomName = addRoom.get("roomName").toString();


        String roomDesc = addRoom.get("roomDesc").toString();
        roomDesc = StringUtils.remove(roomDesc, "[");
        roomDesc = StringUtils.remove(roomDesc, "]");
       
        String roomPic = addRoom.get("roomPic").toString();

        Room room = new Room();
        room.setRoomName(roomName);
        room.setRoomDesc(roomDesc);
        room.setRoomPic("/image/" + roomPic);
        room.setResourceId(102);
        roomService.insertRoom(room);
        return new ResponseDto(200, "添加会议室成功");
    }

    @PutMapping("/room/{roomId}/{status}")
    public ResponseDto updateRoomStatus(@PathVariable String roomId, @PathVariable String status) {
        Room room = new Room();
        Integer roomId_ = Integer.parseInt(roomId);
        room.setRoomId(roomId_);
        room.setIsActive(status);
        roomService.updateRoomStatus(room);
        return new ResponseDto(200, "更新会议室状态成功");
    }

    /**
     * @return
     * @Description 根据roomId更新会议室
     * @Date ${time} ${date}
     * @Param
     **/
    @PutMapping("/room/{roomId}")
    public ResponseDto updateRoom(@PathVariable String roomId, @RequestBody Map<String, Object> map) {

        Integer roomId_ = Integer.parseInt(roomId);

        String roomName = map.get("roomName").toString();

        Room room = new Room();

        //前端传回的参数类型是[xxx,xxx,xxx],处理一下去掉'[' ']'
        String roomDesc = map.get("roomDesc").toString();
        roomDesc = StringUtils.remove(roomDesc, "[");
        roomDesc = StringUtils.remove(roomDesc, "]");

        if (map.get("roomPic") != null) {
            String roomPic = map.get("roomPic").toString();
            room.setRoomPic(roomPic);
        }


        room.setRoomId(roomId_);
        room.setRoomName(roomName);
        roomService.update(room);

        return new ResponseDto(200, "更新成功");
    }
    
    
    /**
     * @Description 根据roomId删除会议室
     * @Date ${time} ${date}
     * @Param 
     * @return 
     **/
    @DeleteMapping("/room/{roomId}")
    public ResponseDto deleteRoom(@PathVariable String roomId) {
        Integer roomId_ = Integer.parseInt(roomId);
        roomService.deleteRoomById(roomId_);

        return new ResponseDto(200, "删除成功");

    }
}


